import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Button, Form, Input, Radio, Select } from 'antd';
import { Link } from 'react-router-dom';
import action from '../../../store/action';
import Transfer from '../Transfer';
import { openNotification } from '../../../component/Notification';

class index extends React.Component {
    constructor() {
        super();
    }
    render() {
        const { Option } = Select;
        const { TextArea } = Input;
        const layout = {
            labelCol: {
                span: 5,
            },
            wrapperCol: {
                span: 15,
            },
        };
        const tailLayout = {
            wrapperCol: {
                offset: 3,
                span: 15,
            },
        };
        const data = this.props.manage_EditData[0];

  

        const onFinish = values => {
            console.log(values);
            let newList;
            let new_val = this.props.manage_SaveVal.concat(this.props.manage_Second_SaveVal);

            if (values.usertype == '3') {
                newList = values.transfer.map((item, index) => {
                    let trans_list = {};
                    let user_propsVal = {};
                    let num=Number(item);
                    user_propsVal.userCode = this.props.manage_EditData[0].code;
                    user_propsVal.type = 2;
                    user_propsVal.targetId =new_val[num].code;
                    return Object.assign(trans_list, user_propsVal)
                })
                
            }
            let list = {
                code: this.props.manage_EditData[0].code,
                lname: values.lname,
                usertype: values.usertype,
                name: values.name,
                professionalDirection: values.professionalDirection,
                sex: values.sex,
                titleOfTechnicalPost: values.titleOfTechnicalPost,
                mail: values.mail,
                remark: values.remark,
            }
            let userList = {
                user: list,
                userOrganizationAndUserPermissionsList: newList
            }
            console.log(userList);
            this.$axios.get('/manager/api/islNameExist', this.$qs.stringify({ name: values.lname })).then((res) => {
                if (res) {
                    this.$axios.post('/manager/api/updateUser', userList).then((res) => {
                        if(res.status==1){
                            this.props.history.push('/admin/Management')
                        }
                    }).catch((err) => {
                        console.log(err);
                    })
                }
              

            }).catch((err) => {
             
                openNotification('该用户名已存在!')
            })
        }

        const onFinishFailed = errorInfo => {
            console.log(errorInfo);
        };

        return <div>
            <div style={{ textAlign: 'center', fontSize: 20 }}>
                修改用户信息
        </div>
            <div style={{ margin: 30 }}>
                <Form
                    {...layout}
                    name="basic"
                    initialValue={{
                        remember: true,
                    }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                >
                    <div className="add_flex">
                        <Form.Item
                            name="usertype"
                            label="类型:"
                            rules={[
                                {
                                    required: true,
                                    message: '内容不能为空!',
                                },
                            ]}
                            initialValue={data.usertype === 1 ? '1' : '3'}
                        >
                            <Select style={{ width: 120 }} disabled>
                                <Option value="1">管理员</Option>
                                <Option value="3">普通用户</Option>

                            </Select>
                        </Form.Item>

                        <Form.Item name="lname" label="用户名:" rules={[
                            {
                                required: true,
                                message: '内容不能为空!',
                            },
                        ]}
                            initialValue={data.lname}
                        >
                            <Input type="textarea" placeholder='' disabled />
                        </Form.Item>
                    </div>
                    <div className='add_flex'>
                        <Form.Item name="name" label="姓名:" rules={[
                            {
                                required: true,
                                message: '内容不能为空!',
                            },
                        ]}
                            initialValue={data.name}
                        >
                            <Input type="textarea" placeholder='请输入姓名' />
                        </Form.Item>
                        <Form.Item name="sex" label="性别:" rules={[
                            {
                                required: true,
                                message: '性别必选!',
                            },
                        ]} initialValue={data.sex === 1 ? '1' : '2'}>
                            <Radio.Group>
                                <Radio value='1'>男</Radio>
                                <Radio value='2'>女</Radio>

                            </Radio.Group>
                        </Form.Item>
                    </div>
                    <div className='add_flex'>
                        <Form.Item name="titleOfTechnicalPost" label="职称:" rules={[
                            {
                                required: true,
                                message: '内容不能为空!',
                            },
                        ]}
                            initialValue={data.titleOfTechnicalPost}
                        >
                            <Input type="textarea" placeholder='请输入职称' />
                        </Form.Item>
                        <Form.Item name="mail" label="邮箱:" rules={[
                            {
                                required: true,
                                message: '内容不能为空!',
                            },
                        ]}
                            initialValue={data.mail}
                        >
                            <Input type="textarea" placeholder='请输入邮箱地址' />
                        </Form.Item>
                    </div>

                    <div className='add_flex'>
                        <Form.Item name="professionalDirection" label="专业方向:" rules={[
                            {
                                required: true,
                                message: '内容不能为空!',
                            },
                        ]}
                            initialValue={data.professionalDirection}
                        >
                            <Input type="textarea" placeholder='请输入专业方向' />
                        </Form.Item>
                        <Form.Item name="remark" label="简介:"
                        initialValue={data.remark}
                        >
                
                            <TextArea  allowClear  autoSize={true}/>
                        </Form.Item>
                    </div>

                    {
                        data.usertype === 3 ? <Transfer style={{ width: 400, margin: '0 auto', }} /> : null
                    }

                    <Form.Item {...tailLayout}>
                        <Button type="primary" htmlType="submit" style={{ margin: '20px 10px' }}>
                            保存
                </Button>
                        <Link to='/admin/Management'><Button type='primary'>返回</Button></Link>
                    </Form.Item>
                </Form>

            </div>
        </div>
    }
}

export default connect(state => ({ ...state.custom }), action.custom)(index);